<?php

class CommentController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','save'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView()
	{
		$issue= $this->loadModel();
		$comment= $this->createComment($issue);
		//echo $_POST['edit_comment'];
		// if(isset($_POST['edit_comment']) and $_POST['edit_comment'] != null){
		// 	$this->renderPartial('_comment',array'comment'=));
		// }

		$this->render('view',array(
			//'model'=>$this->loadModel($id),
			'model' =>$issue,
			'comment'=>$comment,
		));
	}

	public function actionSave()
	{
		//echo $_POST['eid'];

		if(isset($_POST['edithidden'])) {
			foreach ($_POST['edithidden'] as $key) {
			$c_id= $key;
			$c_content= $_POST['edit_'.$c_id];
			$update= Yii::app()->db->createCommand("UPDATE tbl_comment SET content='$c_content' WHERE id=$c_id")->execute();
			//echo "<input type ='text' name='edit_$c_id' id='c.$c_id' value ='$c_content' />";
		}
	}
	}

	public function actionDeleteone()
	{
		if(isset($_POST['pid'])) {
			$c_id= $_POST['pid'];
			$delete= Yii::app()->db->createCommand("DELETE from tbl_comment WHERE id=$c_id")->execute();
		}
	}
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$comment= new Comment;
		//$model= Comment::model()->findByPk($id);
		if(isset($_POST['Comment'])) {
			$comment->attributes= $_POST['Comment'];
			$comment->user_id= Yii::app()->user->id;
			/*if(isset($_GET['Comment']['job_id'])) {
				echo "job active";
				$comment->servive_id= 0;
			}
			if(isset($_GET['Comment']['service_id'])) {
				echo "service active";
				$comment->job_id= 0;
			}*/
			$comment->save();
			$this->renderPartial('_comments-up',array('comment'=>$comment), false, true);
		}
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Comment('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Comment']))
			$model->attributes=$_GET['Comment'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Comment::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	//creatComment
	protected function createComment($issue) {
		$comment= new Comment;
		if(isset($_POST['Comment'])) {
			$comment->attributes= $_POST['Comment'];
			if($issue->addComment($comment)) {
				Yii::app()-> user-> setFlast('commentSubmitted', "Your comment has been added");
				$this->refresh();
			}
		}
		return $comment;
	}
}
